package com.lzymall.controller.backend;

import com.google.common.collect.Maps;
import com.lzymall.common.Const;
import com.lzymall.common.ResponseCode;
import com.lzymall.common.ServiceResponse;
import com.lzymall.pojo.Product;
import com.lzymall.pojo.User;
import com.lzymall.service.IFileService;
import com.lzymall.service.IProductService;
import com.lzymall.service.IUserService;
import com.lzymall.util.CookieUtil;
import com.lzymall.util.JsonUtil;
import com.lzymall.util.PropertiesUtil;
import com.lzymall.util.RedisShardedPoolUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @Author: lizhenyao
 * @Desciption:
 */
@Controller
@RequestMapping("/manage/product")
public class ProductManageController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private IProductService iProductService;

    @Autowired
    private IFileService iFileService;

    @RequestMapping(value = "save.do")
    @ResponseBody
    public ServiceResponse productSave(HttpServletRequest request, Product product){
//        String loginToken = CookieUtil.readLoginToken(request);
//        if (StringUtils.isBlank(loginToken)){
//            return ServiceResponse.createByErrorMessage("用户未登录,无法获取到当前的用户信息");
//        }
//        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
//        User user = JsonUtil.string2Obj(userJsonStr, User.class);
//        if (user == null){
//            return ServiceResponse.createByErrorMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录管理员");
//        }
//        //判断管理员
//        if (iUserService.checkAdminRole(user).isSuccess()){
//            //为管理员,增加操作的逻辑
//            return iProductService.saveOrUpdateProduct(product);
//        }else {
//            return ServiceResponse.createByErrorMessage("无权限操作");
//        }

        //权限判断是否登录或者管理的逻辑交给了拦截器来处理
        return iProductService.saveOrUpdateProduct(product);
    }

    @RequestMapping(value = "set_sale_status.do")
    @ResponseBody
    public ServiceResponse setSaleStatus(HttpServletRequest request, Integer productId, Integer status){

//        String loginToken = CookieUtil.readLoginToken(request);
//        if (StringUtils.isBlank(loginToken)){
//            return ServiceResponse.createByErrorMessage("用户未登录,无法获取到当前的用户信息");
//        }
//        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
//        User user = JsonUtil.string2Obj(userJsonStr, User.class);
//        if (user == null){
//            return ServiceResponse.createByErrorMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录管理员");
//        }
//        //判断管理员
//        if (iUserService.checkAdminRole(user).isSuccess()){
//            //为管理员,增加操作的逻辑
//            return iProductService.setSaleStatus(productId, status);
//        }else {
//            return ServiceResponse.createByErrorMessage("无权限操作");
//        }

        //权限判断是否登录或者管理的逻辑交给了拦截器来处理
        return iProductService.setSaleStatus(productId, status);
    }

    @RequestMapping(value = "detail.do")
    @ResponseBody
    public ServiceResponse detail(HttpServletRequest request, Integer productId){

//        String loginToken = CookieUtil.readLoginToken(request);
//        if (StringUtils.isBlank(loginToken)){
//            return ServiceResponse.createByErrorMessage("用户未登录,无法获取到当前的用户信息");
//        }
//        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
//        User user = JsonUtil.string2Obj(userJsonStr, User.class);
//        if (user == null){
//            return ServiceResponse.createByErrorMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录管理员");
//        }
//        //判断管理员
//        if (iUserService.checkAdminRole(user).isSuccess()){
//            //为管理员,增加操作的逻辑
//            return iProductService.manageProductDetail(productId);
//        }else {
//            return ServiceResponse.createByErrorMessage("无权限操作");
//        }

        //权限判断是否登录或者管理的逻辑交给了拦截器来处理
        return iProductService.manageProductDetail(productId);
    }

    @RequestMapping(value = "list.do")
    @ResponseBody
    public ServiceResponse getList(HttpServletRequest request, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "10")Integer pageSize){
//        String loginToken = CookieUtil.readLoginToken(request);
//        if (StringUtils.isBlank(loginToken)){
//            return ServiceResponse.createByErrorMessage("用户未登录,无法获取到当前的用户信息");
//        }
//        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
//        User user = JsonUtil.string2Obj(userJsonStr, User.class);
//        if (user == null){
//            return ServiceResponse.createByErrorMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录管理员");
//        }
//        //判断管理员
//        if (iUserService.checkAdminRole(user).isSuccess()){
//            //填充业务
//            return iProductService.getProductList(pageNum, pageSize);
//        }else {
//            return ServiceResponse.createByErrorMessage("无权限操作");
//        }

        //权限判断是否登录或者管理的逻辑交给了拦截器来处理
        return iProductService.getProductList(pageNum, pageSize);
    }

    @RequestMapping(value = "search.do")
    @ResponseBody
    public ServiceResponse productSearch(HttpServletRequest request, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "10")Integer pageSize,
                                         Integer productId, String productName){
//        String loginToken = CookieUtil.readLoginToken(request);
//        if (StringUtils.isBlank(loginToken)){
//            return ServiceResponse.createByErrorMessage("用户未登录,无法获取到当前的用户信息");
//        }
//        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
//        User user = JsonUtil.string2Obj(userJsonStr, User.class);
//        if (user == null){
//            return ServiceResponse.createByErrorMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录管理员");
//        }
//        //判断管理员
//        if (iUserService.checkAdminRole(user).isSuccess()){
//            //填充业务
//            return iProductService.searchProduct(productName, productId, pageNum, pageSize);
//        }else {
//            return ServiceResponse.createByErrorMessage("无权限操作");
//        }

        //权限判断是否登录或者管理的逻辑交给了拦截器来处理
        return iProductService.searchProduct(productName, productId, pageNum, pageSize);
    }
    @RequestMapping(value = "upload.do")
    @ResponseBody
    public ServiceResponse upload(@RequestParam(value = "upload_file", required = false) MultipartFile file, HttpServletRequest request){
//        String loginToken = CookieUtil.readLoginToken(request);
//        if (StringUtils.isBlank(loginToken)){
//            return ServiceResponse.createByErrorMessage("用户未登录,无法获取到当前的用户信息");
//        }
//        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
//        User user = JsonUtil.string2Obj(userJsonStr, User.class);
//        if (user == null){
//            return ServiceResponse.createByErrorMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录管理员");
//        }
//        //判断管理员
//        if (iUserService.checkAdminRole(user).isSuccess()){
//            //获取到项目在服务器的路径,相同于在webapp文件夹
//            String path = request.getSession().getServletContext().getRealPath("upload");
//            String targetFileName = iFileService.upload(file, path);
//            //将文件名与配置的url前缀连接在一起
//            String url = PropertiesUtil.getProperty("ftp.server.http.prefix")+targetFileName;
//
//            //有时实体复用的比较少以及要传回的参数较少的时候,可以使用map来代替实体类
//            Map fileMap = Maps.newHashMap();
//            fileMap.put("uri", targetFileName);
//            fileMap.put("url", url);
//            return ServiceResponse.createBySuccess(fileMap);
//        }else {
//            return ServiceResponse.createByErrorMessage("无权限操作");
//        }

        //权限判断是否登录或者管理的逻辑交给了拦截器来处理
        //获取到项目在服务器的路径,相同于在webapp文件夹
        String path = request.getSession().getServletContext().getRealPath("upload");
        String targetFileName = iFileService.upload(file, path);
        //将文件名与配置的url前缀连接在一起
        String url = PropertiesUtil.getProperty("ftp.server.http.prefix")+targetFileName;

        //有时实体复用的比较少以及要传回的参数较少的时候,可以使用map来代替实体类
        Map fileMap = Maps.newHashMap();
        fileMap.put("uri", targetFileName);
        fileMap.put("url", url);
        return ServiceResponse.createBySuccess(fileMap);
    }
    @RequestMapping(value = "richtext_img_upload.do")
    @ResponseBody
    public Map richtextImgUpload(@RequestParam(value = "upload_file", required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response){
//        Map map = Maps.newHashMap();
//        String loginToken = CookieUtil.readLoginToken(request);
//        if (StringUtils.isBlank(loginToken)){
//            map.put("success", false);
//            map.put("msg", "请登录管理员");
//            return map;
//        }
//        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
//        User user = JsonUtil.string2Obj(userJsonStr, User.class);
//        if (user == null){
//            map.put("success", false);
//            map.put("msg", "请登录管理员");
//            return map;
//        }
//        //判断管理员
//        if (iUserService.checkAdminRole(user).isSuccess()){
//            //获取到项目在服务器的路径,相同于在webapp文件夹
//            String path = request.getSession().getServletContext().getRealPath("upload");
//            String targetFileName = iFileService.upload(file, path);
//            if (StringUtils.isBlank(targetFileName)){
//                map.put("success", false);
//                map.put("msg", "上传失败");
//                return map;
//            }
//            //将文件名与配置的url前缀连接在一起
//            String url = PropertiesUtil.getProperty("ftp.server.http.prefix")+targetFileName;
//            map.put("success", true);
//            map.put("msg", "上传成功");
//            map.put("file_path", url);
//            response.addHeader("Access-Control-Allow-Headers", "X-File-Name");
//            return map;
//        }else {
//            map.put("success", false);
//            map.put("msg", "无权限操作");
//            return map;
//        }

        //权限判断是否登录或者管理的逻辑交给了拦截器来处理
        //获取到项目在服务器的路径,相同于在webapp文件夹
        Map map = Maps.newHashMap();
        String path = request.getSession().getServletContext().getRealPath("upload");
        String targetFileName = iFileService.upload(file, path);
        if (StringUtils.isBlank(targetFileName)){
            map.put("success", false);
            map.put("msg", "上传失败");
            return map;
        }
        //将文件名与配置的url前缀连接在一起
        String url = PropertiesUtil.getProperty("ftp.server.http.prefix")+targetFileName;
        map.put("success", true);
        map.put("msg", "上传成功");
        map.put("file_path", url);
        response.addHeader("Access-Control-Allow-Headers", "X-File-Name");
        return map;
    }
}
